﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Code.Models;

namespace Code.BLL
{
    public class UserOptionManage:ManageBase<Option_User>
    {
        /// <summary>
        /// 修改Key所对应的Option，没有时则添加一条
        /// </summary>
        /// <param name="uid">当前用户的编号</param>
        /// <param name="key">Option的Key</param>
        /// <param name="value">Option的Value</param>
        /// <returns></returns>
        public int Update(Guid uid, Option_User_Key key, string value)
        {
            if (string.IsNullOrEmpty(value))
                throw new ArgumentNullException("value");
            var flag = base.Select(s => s.Where(p => p.UserID == uid).Select(p => p.Key).Contains(key.ToString()));
            if (flag)
            {
                NameValue<string, object> update = new NameValue<string, object>()
                {
                    {"value",value}
                };
                NameValue<string, object> where = new NameValue<string, object>()
                {
                    {"Key",key.ToString()},
                    {"UserID",uid}
                };
                return base.Update(update, where);
            }
            else
            {
                NameValue<string, object> insertIetm = new NameValue<string, object>()
                {
                    {"key",key.ToString()},
                    {"value",value},
                    {"UserID",uid}
                };
                return base.Insert(insertIetm);
            }
        }
        /// <summary>
        /// 获取当前用户对应key的Option
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public Option_User this[Guid uid,Option_User_Key key]
        {
            get
            {
                return base.Select(s => s.FirstOrDefault(p =>p.UserID==uid&&p.Key == key.ToString()));
            }
        }


    }
}
